package com.yaozheng.vocationaltraining.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.bean.HundredQuestionsEncyclopediaItemBean;

import java.util.List;

/**
 * Created by moon on 2017/11/9.
 */

public class MeQuestionsEncyclopediaAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//
    private Activity mContext;

    private List<HundredQuestionsEncyclopediaItemBean> data = null;

    private int selectedPosition = -1;

    public MeQuestionsEncyclopediaAdapter(Activity context,
                                               List<HundredQuestionsEncyclopediaItemBean> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.me_question_list_item, null);
            holder = new ViewHolder();

            holder.edit_imageview = (ImageView) convertView
                    .findViewById(R.id.edit_imageview);
            holder.have_adopt_imageview = (ImageView) convertView
                    .findViewById(R.id.have_adopt_imageview);
            holder.cate_name_textview = (TextView) convertView
                    .findViewById(R.id.cate_name_textview);
            holder.title_textview = (TextView) convertView
                    .findViewById(R.id.title_textview);

            holder.reply_tip_layout = (LinearLayout) convertView
                    .findViewById(R.id.reply_tip_layout);
            holder.reply_num_textview = (TextView) convertView.findViewById(R.id.reply_num_textview);

            holder.praise_tip_layout = (LinearLayout) convertView.findViewById(R.id.praise_tip_layout);
            holder.praise_num_textview = (TextView) convertView.findViewById(R.id.praise_num_textview);

            holder.reward_tip_layout = (LinearLayout) convertView.findViewById(R.id.reward_tip_layout);
            holder.reward_num_textview = (TextView) convertView.findViewById(R.id.reward_num_textview);

            holder.click_tip_layout = (LinearLayout) convertView.findViewById(R.id.click_tip_layout);
            holder.click_num_textview = (TextView) convertView.findViewById(R.id.click_num_textview);

            holder.time_textview = (TextView) convertView
                    .findViewById(R.id.time_textview);

            convertView.setTag(holder);//
        } else {
            holder = (ViewHolder) convertView.getTag();//
        }

        holder.cate_name_textview.setText(data.get(position).getCateName());
        holder.title_textview.setText(data.get(position).getTitle());

        if (data.get(position).getAnswer() < 1){
            holder.reply_tip_layout.setVisibility(View.GONE);
        }else{
            holder.reply_tip_layout.setVisibility(View.VISIBLE);
            holder.reply_num_textview.setText(data.get(position).getAnswer()+"");
        }

        if(data.get(position).getLike() < 1){
            holder.praise_tip_layout.setVisibility(View.GONE);
        }else{
            holder.praise_tip_layout.setVisibility(View.VISIBLE);
            holder.praise_num_textview.setText(data.get(position).getLike()+"");
        }

        if(data.get(position).getScore() < 1){
            holder.reward_tip_layout.setVisibility(View.GONE);
        }else{
            holder.reward_tip_layout.setVisibility(View.VISIBLE);
            holder.reward_num_textview.setText(data.get(position).getScore() + "");
        }

        if(data.get(position).getClick() < 1){
            holder.click_tip_layout.setVisibility(View.GONE);
        }else{
            holder.click_tip_layout.setVisibility(View.VISIBLE);
            holder.click_num_textview.setText(data.get(position).getClick()+"");
        }

        if(data.get(position).getSelected() == 1){
            holder.have_adopt_imageview.setVisibility(View.VISIBLE);
        }else{
            holder.have_adopt_imageview.setVisibility(View.GONE);
        }

        if(data.get(position).getResolved() == 1){
            holder.edit_imageview.setImageResource(R.drawable.resolved);
            //holder.edit_imageview.setVisibility(View.VISIBLE);
        }else{
            holder.edit_imageview.setImageResource(R.drawable.not_resolved);
            //holder.edit_imageview.setVisibility(View.GONE);
        }


        holder.time_textview.setText(data.get(position).getCreatedTime());

        return convertView;
    }

    public void setData(List<HundredQuestionsEncyclopediaItemBean> data) {
        this.data = data;
    }

    public void setselectedPosition(int selectedPosition) {
        notifyDataSetChanged();;
    }

    class ViewHolder {
        ImageView edit_imageview;
        TextView cate_name_textview;
        TextView title_textview;

        LinearLayout reply_tip_layout;
        TextView reply_num_textview;

        LinearLayout praise_tip_layout;
        TextView praise_num_textview;

        LinearLayout reward_tip_layout;
        TextView reward_num_textview;

        LinearLayout click_tip_layout;
        TextView click_num_textview;

        TextView time_textview;

        ImageView have_adopt_imageview;
    }

}
